package com.zh.risk.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zh.common.entity.JSONResult;
import com.zh.risk.entity.IpTable;
import com.zh.risk.entity.RiskMerchTrans;
import com.zh.risk.entity.Strategy;
import com.zh.risk.service.IpTableService;
import com.zh.risk.service.StrategyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description: 策略控制器
 * @Author: Liard
 * @Create in: 2020/8/26 10:22
 * @Version: 1.0.1
 */
@Api(value = "策略控制器", tags = "StrategyController")
@RestController
@RequestMapping("/strategy")
@Slf4j
public class StrategyController {

    @Autowired
    private StrategyService service;


    @ApiOperation(value = "分页查询策略控制器", tags = {"getStrategyList"}, notes = "分页查询策略控制器")
    @GetMapping("/getStrategyList")
    public JSONResult<List<Strategy>> getStrategyList(
            @ApiParam(value = "起始页") Integer pageNo,
            @ApiParam(value = "显示数量页面") Integer pageSize,
            @ApiParam(value = "策略实体信息", name = "strategy") Strategy strategy) {
        JSONResult<List<Strategy>> jsonResult = new JSONResult<>();
        try {
            Page<Strategy> page = new Page<>(pageNo, pageSize);
            IPage<Strategy> result = service.getStrategyList(page, strategy);
            jsonResult.setTotal(result.getTotal());
            jsonResult.setData(result.getRecords());
            jsonResult.setSuccess(true);
            jsonResult.setMessage("查询完成!");
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
        }
        return jsonResult;
    }

    @ApiOperation(value = "添加策略信息", tags = {"addStrategy"}, notes = "添加策略信息")
    @PutMapping("/addStrategy")
    public JSONResult<Object> addStrategy(@ApiParam(value = "策略实体信息", name = "strategy") Strategy strategy) {
        JSONResult<Object> jsonResult = new JSONResult<Object>();
        try {
            if (service.addStrategy(strategy)) {
                jsonResult.setSuccess(true);
                jsonResult.setMessage("添加完成!");
            } else {
                jsonResult.setSuccess(false);
                jsonResult.setMessage("请求错误，请稍后再试.");
            }
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
        }
        return jsonResult;
    }

    @ApiOperation(value = "修改策略信息", tags = {"modStrategy"}, notes = "修改策略信息")
    @PutMapping("/modStrategy")
    public JSONResult<Object> modStrategy(@ApiParam(value = "策略实体信息", name = "strategy") Strategy strategy) {
        JSONResult<Object> jsonResult = new JSONResult<Object>();
        try {
            if (service.modStrategy(strategy)) {
                jsonResult.setSuccess(true);
                jsonResult.setMessage("修改完成!");
            } else {
                jsonResult.setSuccess(false);
                jsonResult.setMessage("请求错误，请稍后再试.");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
        }
        return jsonResult;
    }




    @ApiOperation(value = "删除策略信息", tags = {"delStrategy"}, notes = "删除策略信息")
    @DeleteMapping("/delStrategy")
    public JSONResult<Object> delStrategy(@ApiParam(value = "策略实体信息", name = "strategy") Strategy strategy) {
        JSONResult<Object> jsonResult = new JSONResult<Object>();
        try {
            if (service.delStrategy(strategy)) {
                jsonResult.setSuccess(true);
                jsonResult.setMessage("删除完成!");
            } else {
                jsonResult.setSuccess(false);
                jsonResult.setMessage("请求错误，请稍后再试.");
            }
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
        }
        return jsonResult;
    }


    @ApiOperation(value = "查询已绑定规则集", tags = {"getBindRulers"}, notes = "查询已绑定规则集")
    @GetMapping("/getBindRulers")
    public JSONResult<List<RiskMerchTrans>> getBindRulers(@RequestParam("id")String id) {
        JSONResult<List<RiskMerchTrans>> jsonResult = new JSONResult<>();
        try {
            List<RiskMerchTrans> list = service.getBindRulers(id);
            List<RiskMerchTrans> list1 = service.getNotBindRulers(id);
            list.addAll(list1);
            jsonResult.setData(list);
            jsonResult.setSuccess(true);
            jsonResult.setMessage("查询完成!");
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
        }
        return jsonResult;
    }


    @ApiOperation(value = "绑定策略与规则集", tags = {"bindRuleStrategy"}, notes = "绑定策略与规则集")
    @PutMapping("/bindRuleStrategy")
    public JSONResult<Object> bindRuleStrategy(@RequestParam(value = "ruleIds",required = false) List<String> ruleIds, String strategyId) {
        JSONResult<Object> jsonResult = new JSONResult<Object>();
        try {
            if (service.bindRuleStrategy(ruleIds,strategyId) > 0) {
                jsonResult.setSuccess(true);
                jsonResult.setMessage("添加完成!");
            } else {
                jsonResult.setSuccess(false);
                jsonResult.setMessage("请求错误，请稍后再试.");
            }
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
        }
        return jsonResult;
    }

 /*

    @ApiOperation(value = "批量删除黑白名单列表", tags = {"delBatBlackInfo"}, notes = "批量删除黑白名单列表")
    @DeleteMapping("/delBatBlackInfo")
    @ResponseBody
    public JSONResult<Object> delBatBlackInfo(@ApiParam(value = "主键集合", name = "ids") @RequestBody @RequestParam("ids") List<Integer> ids) {
        JSONResult<Object> jsonResult = new JSONResult<Object>();
        try {
            if (service.delBatBlackInfo(ids) > 0) {
                jsonResult.setSuccess(true);
                jsonResult.setMessage("添加完成!");
            } else {
                jsonResult.setSuccess(false);
                jsonResult.setMessage("请求错误，请稍后再试.");
            }
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
        }
        return jsonResult;
    }*/
}
